package com.rqbl.controller;

import com.rqbl.entity.User;
import com.rqbl.service.UserService;
import com.rqbl.tool.SHA1Password;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.Date;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("loginHtml")
    public String loginHtml(){
        return "forward:/html/login.html";
    }

    @RequestMapping("regHtml")
    public String regHtml(){
        return "forward:/html/reg.html";
    }


    /**
     * 用户登录
     * @param userName
     * @param password
     * @param captcha
     * @param session
     * @return
     */
    @RequestMapping("userLogin")
    @ResponseBody
    public String userLogin(String userName, String password,String captcha, HttpSession session){
        System.out.println(userName);
        String checkCode = (String) session.getAttribute("checkCode");
        //验证码验证
        if(!checkCode.equalsIgnoreCase(captcha)){
            return "验证码错误";
        }

        User user = new User();
        user.setUname(userName);
        user.setUpassword(password);

        //确保信息获取成功
        if(user.getUname() == null || user.getUpassword() == null){
            return "输入信息错误";
        }

        //进行密码加密
        user.setUpassword(SHA1Password.encode(user.getUpassword()));
        //进行登录验证
        user = userService.userLogin(user);
        if(user!=null){
            session.setAttribute("user", user);
            return "登录成功";
        }
        return "登录失败，请检查账号或密码";
    }



    /**
     * 用户注册
     * @param request
     * @return
     */
    @RequestMapping("registerUser")
    @ResponseBody
    public String registerUser(HttpServletRequest request){
        String text = request.getParameter("email");
        String vercode = request.getParameter("vercode");
        String checkCode = (String) request.getSession().getAttribute("emailCode");
        //邮箱验证码验证
        if(!checkCode.equals(vercode)){
            return "验证码错误";
        }
        User user = new User();
        System.out.println(request.getParameter("userName"));
        user.setUname(request.getParameter("userName"));
        user.setUpassword(request.getParameter("password"));
        user.setUemail(request.getParameter("userName"));
        //确保信息获取成功
        if(user.getUname()==null || user.getUemail() == null|| user.getUpassword()==null){
            return "注册失败";
        }
        //进行密码加密
        user.setUpassword(SHA1Password.encode(user.getUpassword()));
        return userService.addUser(user)>0?  "注册成功":"注册失败";
    }

    /**
     * 更新用户头像文件地址
     */
    @RequestMapping("updateUserPicture")
    @ResponseBody
    public String updateUserPicture(String url, HttpSession session){
        User user = (User)session.getAttribute("user");
        user.setUpictureurl(url);
        userService.updateUserPicture(user);
        return "更新成功";
    }

    @RequestMapping("updateUserPsw")
    @ResponseBody
    public String updateUserPsw(String lastPassword, String password,HttpSession session){
        User user = (User)session.getAttribute("user");
        System.out.println(lastPassword + "   " + password);
        if(!SHA1Password.encode(lastPassword).equals(user.getUpassword())){
            return "原密码错误";
        }
        //进行密码加密
        user.setUpassword(SHA1Password.encode(password));
        int i = userService.updateUserPsw(user);
        return i>0?"更新成功":"更新失败";
    }

    @RequestMapping("updateUserInfo")
    @ResponseBody
    public String updateUserInfo(HttpServletRequest request, HttpSession session){
        User user = (User)session.getAttribute("user");
        user.setUname(request.getParameter("name"));
        user.setUsex(request.getParameter("sex"));
        user.setUbirthday(Date.valueOf(request.getParameter("date")));
        user.setUaddress(request.getParameter("city-picker"));
        user.setUoccupation(request.getParameter("occupation"));
        int i = userService.updateUserInfo(user);
        return i>0?"更新成功":"更新失败";
    }


    @RequestMapping("updateUserEmail")
    @ResponseBody
    public String updateUserEmail(String email,String code, HttpSession session){
        if(!session.getAttribute("emailCode").equals(code)){
            return "验证码错误";
        }
        User user = (User)session.getAttribute("user");
        user.setUemail(email);
        int i = userService.updateUserInfo(user);
        return i>0?"更新成功":"更新失败";
    }

}
